﻿using ServerBase.Client;
using ServerBase.Config;
using ServerBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UtilLib;
using UtilPublic.Serialization;
using System.Timers;
using System.Windows.Threading;
using ServerBase.Other;
using System.Dynamic;
using UtilWpf;
using UtilPublic.ApiDispatcher;

namespace ToolClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            App.WdMain = this;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            绑定控制器(this);

            ColumnGridNav.Width = new GridLength(0);
            GridNav.Visibility = Visibility.Hidden;

            PageOn("PageDebug");
            PageOn("PageLogin");

        }
        #region 绑定控制器
        public static void 绑定控制器<T>(T t)
        {
            #region 绑定控制器
            try
            {
                var dict = ApiDpProMgr.GetController(t);
                foreach (var kvp in dict)
                {
                    LunarClientDispatcher.BindEventHandler(kvp.Value, (EProtocolId)kvp.Key);
                }
            }
            catch (Exception ex)
            {
                App.DebugError($"DispatchApiRegister {ex.Message}");
            }
            #endregion
        }
        #endregion

        #region 页面切换
        private void ButtonPage_Checked(object sender, RoutedEventArgs e)
        {
            var button = (RadioButton)sender;
            var pageName = button.Tag.ToString();
            PageOn(pageName);
        }
        public static Dictionary<string, Page> DictPage = new Dictionary<string, Page>();
        public void PageOn(string pageName)
        {
            //if (pageName == "PageShine")
            //{
            //    PageSetWidth(500);
            //}
            //else
            //{
            //    PageSetWidth(1200);
            //}
            if (DictPage.TryGetValue(pageName, out var page))
            {
                FrameMain.Content = page;
                return;
            }
            string uri = "ToolClient." + pageName;
            Type type = Type.GetType(uri);
            if (type != null)
            {
                object obj = type.Assembly.CreateInstance(uri);
                page = obj as Page;
                FrameMain.Content = page;
                DictPage[pageName] = page;
            }
        }
        private void PageSetWidth(double ww)
        {
            if (Width != ww)
            {
                Width = ww;
            }
        }
        #endregion



    }
}
